<?php
class categoryModel extends baseModel{
    protected $table = 'category';

    //公共信息
    public function common_info($id=null)
    {
        if(!empty($id)){
            $info=$this->info($id);
            $file_id=api('editor','get_relation',array('table'=>'category','id'=>$id));
            $action_name='编辑';
            $action='edit';
        }else{
            $file_id=',';
            $action_name='添加';
            $action='add';
        }
        $category_list=model('category')->category_list();
        $model_list=api('expand_model','model_list');
        $tpl_list=model('setting')->tpl_list();
        $filter_class_list=api('filter','class_list');
        $data['info']=$info;
        $data['file_id']=$file_id;
        $data['action_name']=$action_name;
        $data['action']=$action;
        $data['category_list']=$category_list;
        $data['model_list']=$model_list;
        $data['tpl_list']=$tpl_list;
        $data['filter_class_list']=$filter_class_list;
        return $data;
    }

    //获取栏目树形列表
    public function category_list($id = 0) {
        $sql="
        SELECT A.*,B.model,B.name as mname,B.list_type
        FROM {$this->model->pre}category A 
        LEFT JOIN {$this->model->pre}model B ON A.mid = B.mid
        ORDER BY A.sequence DESC,A.cid ASC
        ";
        $data=$this->model->query($sql);
        $cat = new Category(array('cid', 'pid', 'name', 'cname'));
        return $cat->getTree($data, $id);
    }

    //获取子栏目统计
    public function list_count($pid)
    {
        return $this->count('pid='.$pid);
    }

    //获取所有栏目统计
    public function category_count()
    {
        return $this->count();
    }

    //获取栏目基本信息
    public function info($id)
    {
        return $this->find('cid='.$id);
    }

    //字段保存格式化
    public function common_data_save($data){
        $cid=intval($data['cid']);
        $data['urlname']=$this->get_urlname($data['name'],$data['urlname'],$cid);
        $data['seo_content']=html_in($data['seo_content']);
        return $data;

    }

    //栏目保存
    public function add_save($data)
    {   
        //格式化部分字段
        $data=$this->common_data_save($data);
        //录入数据
        $cid=$this->insert($data);
        api('editor','relation',array('table'=>'category','file_id'=>$data['file_id'],'id'=>$cid));
        return $cid;
    }

    //栏目保存
    public function edit_save($data)
    {
        //格式化部分字段
        $data=$this->common_data_save($data);
        //录入数据
        $status=$this->update('cid='.$data['cid'],$data);
        api('editor','relation',array('table'=>'category','file_id'=>$data['file_id'],'id'=>$data['cid']));
        return $status;
    }

    //栏目删除
    public function del($cid)
    {
        $status=$this->delete('cid='.$cid);
        api('editor','del_file',array('table'=>'category','id'=>$cid));
        return $status;
    }

    //获取栏目拼音
    public function get_urlname($name='', $urlname = null, $cid = null)
    {
        if(empty($name)){
            return false;
            exit;
        }
        if (empty($urlname))
        {
            $pinyin = new Pinyin();
            $name = preg_replace('/\s+/', '-', $name);
            $pattern = '/[^\x{4e00}-\x{9fa5}\d\w\-]+/u';
            $name = preg_replace($pattern, '', $name);
            $urlname = substr($pinyin->output($name, true),0,30);
            if(substr($urlname,0,1)=='-'){
                $urlname=substr($urlname,1);
            }
            if(substr($urlname,-1)=='-'){
                $urlname=substr($urlname,0,-1);
            }
        }

        $where='';
        if (!empty($cid))
        {
            $where = 'AND cid<>' . $cid;
        }
        
        $info = $this->count("urlname='".$urlname."'" .$where); 

        if (empty($info))
        {
            return $urlname;
        }
        else
        {
            return $urlname.substr(cp_uniqid(),8);
        }
    }


    public function ajax_edit($cid,$name,$content){
        $data[$name]=$content;
        return $this->update('cid='.$cid,$data);
        
    }

}

?>